home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume6 / rpc2 / part08 < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  51.3 KB

  1. Subject:  v06i096:  Sun RPC Source (rpc2), Part08/11
  2. Newsgroups: mod.sources
  3. Approved: rs@mirror.UUCP
  4.  
  5. Submitted by: cca!SUN.COM!marks (Mark Stein)
  6. Mod.sources: Volume 6, Issue 96
  7. Archive-name: rpc2/Part08
  8.  
  9. [  I have not tried any of this code, just verified that it unpacks ok.  -r$ ]
  10.  
  11. Sun RPC source (part 8 of 11).  This software package contains code
  12. and documentation for Revision 3.0 of the Sun Remote Procedure Call
  13. library.  In addition, a beta version of the XDR/RPC protocol compiler
  14. is included.  Comments about this latest release may be mailed to
  15. sun!rpc or rpc@sun.com.
  16.  
  17. Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  18. unrestricted use provided that this legend is included on all tape
  19. media and as a part of the software program in whole or part.  Users
  20. may copy or modify Sun RPC without charge, but are not authorized to
  21. license or distribute it to anyone else except as part of a product or
  22. program developed by the user.
  23.  
  24. - - - - - - - - - C U T - H E R E - - - - - - - - - - - - - - - - - -
  25. #! /bin/sh
  26. # This is a shell archive, meaning:
  27. # 1. Remove everything above the #! /bin/sh line.
  28. # 2. Save the resulting text in a file.
  29. # 3. Execute the file with /bin/sh (not csh) to create:
  30. #    rpc/rpclib/svc_auth.c
  31. #    rpc/rpclib/svc_auth_unix.c
  32. #    rpc/rpclib/svc_raw.c
  33. #    rpc/rpclib/svc_simple.c
  34. #    rpc/rpclib/svc_tcp.c
  35. #    rpc/rpclib/svc_udp.c
  36. #    rpc/rpclib/xdr.c
  37. #    rpc/rpclib/xdr_array.c
  38. # This archive created: Mon Jul 14 16:55:34 1986
  39. export PATH; PATH=/bin:/usr/bin:$PATH
  40. for d in rpc rpc/doc rpc/rpclib rpc/tools rpc/toys rpc/rpclib/profiled rpc/rpcgen rpc/rpcgen/test
  41. do
  42.     if test ! -d $d
  43.     then
  44.         echo "shar: Making directory $d"
  45.         mkdir $d
  46.         chmod 755 $d
  47.     fi
  48. done
  49. echo shar: "extracting 'rpc/rpclib/svc_auth.c'" '(3678 characters)'
  50. if test -f 'rpc/rpclib/svc_auth.c'
  51. then
  52.     echo shar: "will not over-write existing file 'rpc/rpclib/svc_auth.c'"
  53. else
  54. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/svc_auth.c'
  55. X/*
  56. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  57. X * unrestricted use provided that this legend is included on all tape
  58. X * media and as a part of the software program in whole or part.  Users
  59. X * may copy or modify Sun RPC without charge, but are not authorized
  60. X * to license or distribute it to anyone else except as part of a product or
  61. X * program developed by the user.
  62. X * 
  63. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  64. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  65. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  66. X * 
  67. X * Sun RPC is provided with no support and without any obligation on the
  68. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  69. X * modification or enhancement.
  70. X * 
  71. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  72. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  73. X * OR ANY PART THEREOF.
  74. X * 
  75. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  76. X * or profits or other special, indirect and consequential damages, even if
  77. X * Sun has been advised of the possibility of such damages.
  78. X * 
  79. X * Sun Microsystems, Inc.
  80. X * 2550 Garcia Avenue
  81. X * Mountain View, California  94043
  82. X */
  83. X#ifndef lint
  84. Xstatic char sccsid[] = "@(#)svc_auth.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  85. X#endif
  86. X
  87. X/*
  88. X * svc_auth.c, Server-side rpc authenticator interface.
  89. X *
  90. X * Copyright (C) 1984, Sun Microsystems, Inc.
  91. X */
  92. X
  93. X#include "types.h"
  94. X#include <netinet/in.h>
  95. X#include "xdr.h"
  96. X#include "auth.h"
  97. X#include "clnt.h"
  98. X#include "rpc_msg.h"
  99. X#include "svc.h"
  100. X#include "svc_auth.h"
  101. X
  102. X/*
  103. X * svcauthsw is the bdevsw of server side authentication. 
  104. X * 
  105. X * Server side authenticators are called from authenticate by
  106. X * using the client auth struct flavor field to index into svcauthsw.
  107. X * The server auth flavors must implement a routine that looks  
  108. X * like: 
  109. X * 
  110. X *    enum auth_stat 
  111. X *    flavorx_auth(rqst, msg)
  112. X *        register struct svc_req *rqst; 
  113. X *        register struct rpc_msg *msg;
  114. X *  
  115. X */
  116. X
  117. Xenum auth_stat _svcauth_null();        /* no authentication */
  118. Xenum auth_stat _svcauth_unix();        /* unix style (uid, gids) */
  119. Xenum auth_stat _svcauth_short();    /* short hand unix style */
  120. X
  121. Xstatic struct {
  122. X    enum auth_stat (*authenticator)();
  123. X} svcauthsw[] = {
  124. X    _svcauth_null,            /* AUTH_NULL */
  125. X    _svcauth_unix,            /* AUTH_UNIX */
  126. X    _svcauth_short            /* AUTH_SHORT */
  127. X};
  128. X#define    AUTH_MAX    2        /* HIGHEST AUTH NUMBER */
  129. X
  130. X
  131. X/*
  132. X * The call rpc message, msg has been obtained from the wire.  The msg contains
  133. X * the raw form of credentials and verifiers.  authenticate returns AUTH_OK
  134. X * if the msg is successfully authenticated.  If AUTH_OK then the routine also
  135. X * does the following things:
  136. X * set rqst->rq_xprt->verf to the appropriate response verifier;
  137. X * sets rqst->rq_client_cred to the "cooked" form of the credentials.
  138. X *
  139. X * NB: rqst->rq_cxprt->verf must be pre-alloctaed;
  140. X * its length is set appropriately.
  141. X *
  142. X * The caller still owns and is responsible for msg->u.cmb.cred and
  143. X * msg->u.cmb.verf.  The authentication system retains ownership of
  144. X * rqst->rq_client_cred, the cooked credentials.
  145. X */
  146. Xenum auth_stat
  147. X_authenticate(rqst, msg)
  148. X    register struct svc_req *rqst;
  149. X    struct rpc_msg *msg;
  150. X{
  151. X    register int cred_flavor;
  152. X
  153. X    rqst->rq_cred = msg->rm_call.cb_cred;
  154. X    rqst->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
  155. X    rqst->rq_xprt->xp_verf.oa_length = 0;
  156. X    cred_flavor = rqst->rq_cred.oa_flavor;
  157. X    if (cred_flavor <= AUTH_MAX) {
  158. X        return ((*(svcauthsw[cred_flavor].authenticator))(rqst, msg));
  159. X    }
  160. X
  161. X    return (AUTH_REJECTEDCRED);
  162. X}
  163. X
  164. Xenum auth_stat
  165. X_svcauth_null(/*rqst, msg*/)
  166. X    /*struct svc_req *rqst;
  167. X    struct rpc_msg *msg;*/
  168. X{
  169. X
  170. X    return (AUTH_OK);
  171. X}
  172. SHAR_EOF
  173. if test 3678 -ne "`wc -c < 'rpc/rpclib/svc_auth.c'`"
  174. then
  175.     echo shar: "error transmitting 'rpc/rpclib/svc_auth.c'" '(should have been 3678 characters)'
  176. fi
  177. chmod 444 'rpc/rpclib/svc_auth.c'
  178. fi
  179. echo shar: "extracting 'rpc/rpclib/svc_auth_unix.c'" '(4210 characters)'
  180. if test -f 'rpc/rpclib/svc_auth_unix.c'
  181. then
  182.     echo shar: "will not over-write existing file 'rpc/rpclib/svc_auth_unix.c'"
  183. else
  184. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/svc_auth_unix.c'
  185. X/*
  186. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  187. X * unrestricted use provided that this legend is included on all tape
  188. X * media and as a part of the software program in whole or part.  Users
  189. X * may copy or modify Sun RPC without charge, but are not authorized
  190. X * to license or distribute it to anyone else except as part of a product or
  191. X * program developed by the user.
  192. X * 
  193. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  194. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  195. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  196. X * 
  197. X * Sun RPC is provided with no support and without any obligation on the
  198. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  199. X * modification or enhancement.
  200. X * 
  201. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  202. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  203. X * OR ANY PART THEREOF.
  204. X * 
  205. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  206. X * or profits or other special, indirect and consequential damages, even if
  207. X * Sun has been advised of the possibility of such damages.
  208. X * 
  209. X * Sun Microsystems, Inc.
  210. X * 2550 Garcia Avenue
  211. X * Mountain View, California  94043
  212. X */
  213. X#ifndef lint
  214. Xstatic char sccsid[] = "@(#)svc_auth_unix.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  215. X#endif
  216. X
  217. X/*
  218. X * svc_auth_unix.c
  219. X * Handles UNIX flavor authentication parameters on the service side of rpc.
  220. X * There are two svc auth implementations here: AUTH_UNIX and AUTH_SHORT.
  221. X * _svcauth_unix does full blown unix style uid,gid+gids auth,
  222. X * _svcauth_short uses a shorthand auth to index into a cache of longhand auths.
  223. X * Note: the shorthand has been gutted for efficiency.
  224. X *
  225. X * Copyright (C) 1984, Sun Microsystems, Inc.
  226. X */
  227. X
  228. X#include <stdio.h>
  229. X#include "types.h"
  230. X#include <sys/time.h>
  231. X#include <netinet/in.h>
  232. X#include "xdr.h"
  233. X#include "auth.h"
  234. X#include "clnt.h"
  235. X#include "rpc_msg.h"
  236. X#include "svc.h"
  237. X#include "auth_unix.h"
  238. X#include "svc_auth.h"
  239. Xchar *mem_alloc();
  240. X
  241. X#define    RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
  242. X           * BYTES_PER_XDR_UNIT)
  243. X
  244. X/*
  245. X * Unix longhand authenticator
  246. X */
  247. Xenum auth_stat
  248. X_svcauth_unix(rqst, msg)
  249. X    register struct svc_req *rqst;
  250. X    register struct rpc_msg *msg;
  251. X{
  252. X    register enum auth_stat stat;
  253. X    XDR xdrs;
  254. X    register struct authunix_parms *aup;
  255. X    register long *buf;
  256. X    struct area {
  257. X        struct authunix_parms area_aup;
  258. X        char area_machname[MAX_MACHINE_NAME];
  259. X        int area_gids[NGRPS];
  260. X    } *area;
  261. X    u_int auth_len;
  262. X    int str_len, gid_len;
  263. X    register int i;
  264. X
  265. X    area = (struct area *) rqst->rq_clntcred;
  266. X    aup = &area->area_aup;
  267. X    aup->aup_machname = area->area_machname;
  268. X    aup->aup_gids = area->area_gids;
  269. X    auth_len = (u_int)msg->rm_call.cb_cred.oa_length;
  270. X    xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE);
  271. X    buf = XDR_INLINE(&xdrs, auth_len);
  272. X    if (buf != NULL) {
  273. X        aup->aup_time = IXDR_GET_LONG(buf);
  274. X        str_len = IXDR_GET_U_LONG(buf);
  275. X        bcopy(buf, aup->aup_machname, str_len);
  276. X        aup->aup_machname[str_len] = 0;
  277. X        str_len = RNDUP(str_len);
  278. X        buf += str_len / sizeof (long);
  279. X        aup->aup_uid = IXDR_GET_LONG(buf);
  280. X        aup->aup_gid = IXDR_GET_LONG(buf);
  281. X        gid_len = IXDR_GET_U_LONG(buf);
  282. X        if (gid_len > NGRPS) {
  283. X            stat = AUTH_BADCRED;
  284. X            goto done;
  285. X        }
  286. X        aup->aup_len = gid_len;
  287. X        for (i = 0; i < gid_len; i++) {
  288. X            aup->aup_gids[i] = IXDR_GET_LONG(buf);
  289. X        }
  290. X        /*
  291. X         * five is the smallest unix credentials structure -
  292. X         * timestamp, hostname len (0), uid, gid, and gids len (0).
  293. X         */
  294. X        if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) {
  295. X            printf("bad auth_len gid %d str %d auth %d\n",
  296. X                   gid_len, auth_len, auth_len);
  297. X            stat = AUTH_BADCRED;
  298. X            goto done;
  299. X        }
  300. X    } else if (! xdr_authunix_parms(&xdrs, aup)) {
  301. X        xdrs.x_op = XDR_FREE;
  302. X        (void)xdr_authunix_parms(&xdrs, aup);
  303. X        stat = AUTH_BADCRED;
  304. X        goto done;
  305. X    }
  306. X    rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL;
  307. X    rqst->rq_xprt->xp_verf.oa_length = 0;
  308. X    stat = AUTH_OK;
  309. Xdone:
  310. X    XDR_DESTROY(&xdrs);
  311. X    return (stat);
  312. X}
  313. X
  314. X
  315. X/*
  316. X * Shorthand unix authenticator
  317. X * Looks up longhand in a cache.
  318. X */
  319. X/*ARGSUSED*/
  320. Xenum auth_stat 
  321. X_svcauth_short(rqst, msg)
  322. X    struct svc_req *rqst;
  323. X    struct rpc_msg *msg;
  324. X{
  325. X    return (AUTH_REJECTEDCRED);
  326. X}
  327. SHAR_EOF
  328. if test 4210 -ne "`wc -c < 'rpc/rpclib/svc_auth_unix.c'`"
  329. then
  330.     echo shar: "error transmitting 'rpc/rpclib/svc_auth_unix.c'" '(should have been 4210 characters)'
  331. fi
  332. chmod 444 'rpc/rpclib/svc_auth_unix.c'
  333. fi
  334. echo shar: "extracting 'rpc/rpclib/svc_raw.c'" '(3569 characters)'
  335. if test -f 'rpc/rpclib/svc_raw.c'
  336. then
  337.     echo shar: "will not over-write existing file 'rpc/rpclib/svc_raw.c'"
  338. else
  339. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/svc_raw.c'
  340. X/*
  341. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  342. X * unrestricted use provided that this legend is included on all tape
  343. X * media and as a part of the software program in whole or part.  Users
  344. X * may copy or modify Sun RPC without charge, but are not authorized
  345. X * to license or distribute it to anyone else except as part of a product or
  346. X * program developed by the user.
  347. X * 
  348. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  349. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  350. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  351. X * 
  352. X * Sun RPC is provided with no support and without any obligation on the
  353. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  354. X * modification or enhancement.
  355. X * 
  356. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  357. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  358. X * OR ANY PART THEREOF.
  359. X * 
  360. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  361. X * or profits or other special, indirect and consequential damages, even if
  362. X * Sun has been advised of the possibility of such damages.
  363. X * 
  364. X * Sun Microsystems, Inc.
  365. X * 2550 Garcia Avenue
  366. X * Mountain View, California  94043
  367. X */
  368. X#ifndef lint
  369. Xstatic char sccsid[] = "@(#)svc_raw.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  370. X#endif
  371. X
  372. X/*
  373. X * svc_raw.c,   This a toy for simple testing and timing.
  374. X * Interface to create an rpc client and server in the same UNIX process.
  375. X * This lets us similate rpc and get rpc (round trip) overhead, without
  376. X * any interference from the kernal.
  377. X *
  378. X * Copyright (C) 1984, Sun Microsystems, Inc.
  379. X */
  380. X
  381. X#include "types.h"
  382. X#include <netinet/in.h>
  383. X#include "xdr.h"
  384. X#include "auth.h"
  385. X#include "clnt.h"
  386. X#include "rpc_msg.h"
  387. X#include "svc.h"
  388. X
  389. X#define NULL ((caddr_t)0)
  390. X
  391. X/*
  392. X * This is the "network" that we will be moving data over
  393. X */
  394. Xextern char _raw_buf[UDPMSGSIZE];
  395. X
  396. Xstatic bool_t        svcraw_recv();
  397. Xstatic enum xprt_stat     svcraw_stat();
  398. Xstatic bool_t        svcraw_getargs();
  399. Xstatic bool_t        svcraw_reply();
  400. Xstatic bool_t        svcraw_freeargs();
  401. Xstatic void        svcraw_destroy();
  402. X
  403. Xstatic struct xp_ops server_ops = {
  404. X    svcraw_recv,
  405. X    svcraw_stat,
  406. X    svcraw_getargs,
  407. X    svcraw_reply,
  408. X    svcraw_freeargs,
  409. X    svcraw_destroy
  410. X};
  411. X
  412. Xstatic SVCXPRT server;
  413. Xstatic XDR xdr_stream;
  414. Xstatic char verf_body[MAX_AUTH_BYTES];
  415. X
  416. XSVCXPRT *
  417. Xsvcraw_create()
  418. X{
  419. X
  420. X    server.xp_sock = 0;
  421. X    server.xp_port = 0;
  422. X    server.xp_ops = &server_ops;
  423. X    server.xp_verf.oa_base = verf_body;
  424. X    xdrmem_create(&xdr_stream, _raw_buf, UDPMSGSIZE, XDR_FREE);
  425. X    return (&server);
  426. X}
  427. X
  428. Xstatic enum xprt_stat
  429. Xsvcraw_stat()
  430. X{
  431. X
  432. X    return (XPRT_IDLE);
  433. X}
  434. X
  435. Xstatic bool_t
  436. Xsvcraw_recv(xprt, msg)
  437. X    SVCXPRT *xprt;
  438. X    struct rpc_msg *msg;
  439. X{
  440. X    register XDR *xdrs = &xdr_stream;
  441. X
  442. X    xdrs->x_op = XDR_DECODE;
  443. X    XDR_SETPOS(xdrs, 0);
  444. X    if (! xdr_callmsg(xdrs, msg))
  445. X           return (FALSE);
  446. X    return (TRUE);
  447. X}
  448. X
  449. Xstatic bool_t
  450. Xsvcraw_reply(xprt, msg)
  451. X    SVCXPRT *xprt;
  452. X    struct rpc_msg *msg;
  453. X{
  454. X    register XDR *xdrs = &xdr_stream;
  455. X
  456. X    xdrs->x_op = XDR_ENCODE;
  457. X    XDR_SETPOS(xdrs, 0);
  458. X    if (! xdr_replymsg(xdrs, msg))
  459. X           return (FALSE);
  460. X    (void)XDR_GETPOS(xdrs);  /* called just for overhead */
  461. X    return (TRUE);
  462. X}
  463. X
  464. Xstatic bool_t
  465. Xsvcraw_getargs(xprt, xdr_args, args_ptr)
  466. X    SVCXPRT *xprt;
  467. X    xdrproc_t xdr_args;
  468. X    caddr_t args_ptr;
  469. X{
  470. X
  471. X    return ((*xdr_args)(&xdr_stream, args_ptr));
  472. X}
  473. X
  474. Xstatic bool_t
  475. Xsvcraw_freeargs(xprt, xdr_args, args_ptr)
  476. X    SVCXPRT *xprt;
  477. X    xdrproc_t xdr_args;
  478. X    caddr_t args_ptr;
  479. X{ 
  480. X    register XDR *xdrs = &xdr_stream;
  481. X
  482. X    xdrs->x_op = XDR_FREE;
  483. X    return ((*xdr_args)(xdrs, args_ptr));
  484. X} 
  485. X
  486. Xstatic void
  487. Xsvcraw_destroy()
  488. X{
  489. X}
  490. SHAR_EOF
  491. if test 3569 -ne "`wc -c < 'rpc/rpclib/svc_raw.c'`"
  492. then
  493.     echo shar: "error transmitting 'rpc/rpclib/svc_raw.c'" '(should have been 3569 characters)'
  494. fi
  495. chmod 444 'rpc/rpclib/svc_raw.c'
  496. fi
  497. echo shar: "extracting 'rpc/rpclib/svc_simple.c'" '(3936 characters)'
  498. if test -f 'rpc/rpclib/svc_simple.c'
  499. then
  500.     echo shar: "will not over-write existing file 'rpc/rpclib/svc_simple.c'"
  501. else
  502. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/svc_simple.c'
  503. X/*
  504. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  505. X * unrestricted use provided that this legend is included on all tape
  506. X * media and as a part of the software program in whole or part.  Users
  507. X * may copy or modify Sun RPC without charge, but are not authorized
  508. X * to license or distribute it to anyone else except as part of a product or
  509. X * program developed by the user.
  510. X * 
  511. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  512. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  513. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  514. X * 
  515. X * Sun RPC is provided with no support and without any obligation on the
  516. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  517. X * modification or enhancement.
  518. X * 
  519. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  520. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  521. X * OR ANY PART THEREOF.
  522. X * 
  523. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  524. X * or profits or other special, indirect and consequential damages, even if
  525. X * Sun has been advised of the possibility of such damages.
  526. X * 
  527. X * Sun Microsystems, Inc.
  528. X * 2550 Garcia Avenue
  529. X * Mountain View, California  94043
  530. X */
  531. X#ifndef lint
  532. Xstatic char sccsid[] = "@(#)svc_simple.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  533. X#endif
  534. X
  535. X/* 
  536. X * svc_simple.c
  537. X * Simplified front end to rpc.
  538. X *
  539. X * Copyright (C) 1984, Sun Microsystems, Inc.
  540. X */
  541. X
  542. X#include <stdio.h>
  543. X#include <rpc/rpc.h>
  544. X#include <sys/socket.h>
  545. X#include <sys/time.h>
  546. X#include <netdb.h>
  547. X
  548. Xstatic struct proglst {
  549. X    char *(*p_progname)();
  550. X    int  p_prognum;
  551. X    int  p_procnum;
  552. X    xdrproc_t p_inproc, p_outproc;
  553. X    struct proglst *p_nxt;
  554. X} *proglst;
  555. Xint universal();
  556. Xstatic SVCXPRT *transp;
  557. Xstatic madetransp;
  558. Xstruct proglst *pl;
  559. X
  560. Xregisterrpc(prognum, versnum, procnum, progname, inproc, outproc)
  561. X    char *(*progname)();
  562. X    xdrproc_t inproc, outproc;
  563. X{
  564. X    
  565. X    if (procnum == NULLPROC) {
  566. X        fprintf(stderr,
  567. X            "can't reassign procedure number %d\n", NULLPROC);
  568. X        return (-1);
  569. X    }
  570. X    if (!madetransp) {
  571. X        madetransp = 1;
  572. X        transp = svcudp_create(RPC_ANYSOCK);
  573. X        if (transp == NULL) {
  574. X            fprintf(stderr, "couldn't create an rpc server\n");
  575. X            return (-1);
  576. X        }
  577. X    }
  578. X    pmap_unset(prognum, versnum);
  579. X    if (!svc_register(transp, prognum, versnum, universal, IPPROTO_UDP)) {
  580. X            fprintf(stderr, "couldn't register prog %d vers %d\n",
  581. X            prognum, versnum);
  582. X        return (-1);
  583. X    }
  584. X    pl = (struct proglst *)malloc(sizeof(struct proglst));
  585. X    if (pl == NULL) {
  586. X        fprintf(stderr, "registerrpc: out of memory\n");
  587. X        return (-1);
  588. X    }
  589. X    pl->p_progname = progname;
  590. X    pl->p_prognum = prognum;
  591. X    pl->p_procnum = procnum;
  592. X    pl->p_inproc = inproc;
  593. X    pl->p_outproc = outproc;
  594. X    pl->p_nxt = proglst;
  595. X    proglst = pl;
  596. X    return (0);
  597. X}
  598. X
  599. Xstatic
  600. Xuniversal(rqstp, transp)
  601. X    struct svc_req *rqstp;
  602. X    SVCXPRT *transp;
  603. X{
  604. X    int prog, proc, i;
  605. X    char *outdata;
  606. X    char xdrbuf[UDPMSGSIZE];
  607. X    struct proglst *pl;
  608. X
  609. X    /* 
  610. X     * enforce "procnum 0 is echo" convention
  611. X     */
  612. X    if (rqstp->rq_proc == NULLPROC) {
  613. X        if (svc_sendreply(transp, xdr_void, 0) == FALSE) {
  614. X            fprintf(stderr, "xxx\n");
  615. X            exit(1);
  616. X        }
  617. X        return;
  618. X    }
  619. X    prog = rqstp->rq_prog;
  620. X    proc = rqstp->rq_proc;
  621. X    for (pl = proglst; pl != NULL; pl = pl->p_nxt)
  622. X        if (pl->p_prognum == prog && pl->p_procnum == proc) {
  623. X            /* decode arguments into a CLEAN buffer */
  624. X            bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */
  625. X            if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) {
  626. X                svcerr_decode(transp);
  627. X                return;
  628. X            }
  629. X            outdata = (*(pl->p_progname))(xdrbuf);
  630. X            if (outdata == NULL && pl->p_outproc != xdr_void)
  631. X                /* there was an error */
  632. X                return;
  633. X            if (!svc_sendreply(transp, pl->p_outproc, outdata)) {
  634. X                fprintf(stderr,
  635. X                    "trouble replying to prog %d\n",
  636. X                    pl->p_prognum);
  637. X                exit(1);
  638. X            /* free the decoded arguments */
  639. X            (void)svc_freeargs(transp, pl->p_inproc, xdrbuf);
  640. X            }
  641. X            return;
  642. X        }
  643. X    fprintf(stderr, "never registered prog %d\n", prog);
  644. X    exit(1);
  645. X}
  646. X
  647. SHAR_EOF
  648. if test 3936 -ne "`wc -c < 'rpc/rpclib/svc_simple.c'`"
  649. then
  650.     echo shar: "error transmitting 'rpc/rpclib/svc_simple.c'" '(should have been 3936 characters)'
  651. fi
  652. chmod 444 'rpc/rpclib/svc_simple.c'
  653. fi
  654. echo shar: "extracting 'rpc/rpclib/svc_tcp.c'" '(9984 characters)'
  655. if test -f 'rpc/rpclib/svc_tcp.c'
  656. then
  657.     echo shar: "will not over-write existing file 'rpc/rpclib/svc_tcp.c'"
  658. else
  659. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/svc_tcp.c'
  660. X/*
  661. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  662. X * unrestricted use provided that this legend is included on all tape
  663. X * media and as a part of the software program in whole or part.  Users
  664. X * may copy or modify Sun RPC without charge, but are not authorized
  665. X * to license or distribute it to anyone else except as part of a product or
  666. X * program developed by the user.
  667. X * 
  668. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  669. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  670. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  671. X * 
  672. X * Sun RPC is provided with no support and without any obligation on the
  673. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  674. X * modification or enhancement.
  675. X * 
  676. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  677. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  678. X * OR ANY PART THEREOF.
  679. X * 
  680. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  681. X * or profits or other special, indirect and consequential damages, even if
  682. X * Sun has been advised of the possibility of such damages.
  683. X * 
  684. X * Sun Microsystems, Inc.
  685. X * 2550 Garcia Avenue
  686. X * Mountain View, California  94043
  687. X */
  688. X#ifndef lint
  689. Xstatic char sccsid[] = "@(#)svc_tcp.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  690. X#endif
  691. X
  692. X/*
  693. X * svc_tcp.c, Server side for TCP/IP based RPC. 
  694. X *
  695. X * Copyright (C) 1984, Sun Microsystems, Inc.
  696. X *
  697. X * Actually implements two flavors of transporter -
  698. X * a tcp rendezvouser (a listner and connection establisher)
  699. X * and a record/tcp stream.
  700. X */
  701. X
  702. X#include <stdio.h>
  703. X#include "types.h"
  704. X#include <netinet/in.h>
  705. X#include <sys/socket.h>
  706. X#include <sys/time.h>
  707. X#include <errno.h>
  708. X#include "xdr.h"
  709. X#include "auth.h"
  710. X#include "clnt.h"
  711. X#include "rpc_msg.h"
  712. X#include "svc.h"
  713. Xchar *mem_alloc();
  714. Xextern bool_t abort();
  715. Xextern errno;
  716. X
  717. X/*
  718. X * Ops vector for TCP/IP based rpc service handle
  719. X */
  720. Xstatic bool_t        svctcp_recv();
  721. Xstatic enum xprt_stat    svctcp_stat();
  722. Xstatic bool_t        svctcp_getargs();
  723. Xstatic bool_t        svctcp_reply();
  724. Xstatic bool_t        svctcp_freeargs();
  725. Xstatic void        svctcp_destroy();
  726. X
  727. Xstatic struct xp_ops svctcp_op = {
  728. X    svctcp_recv,
  729. X    svctcp_stat,
  730. X    svctcp_getargs,
  731. X    svctcp_reply,
  732. X    svctcp_freeargs,
  733. X    svctcp_destroy
  734. X};
  735. X
  736. X/*
  737. X * Ops vector for TCP/IP rendezvous handler
  738. X */
  739. Xstatic bool_t        rendezvous_request();
  740. Xstatic enum xprt_stat    rendezvous_stat();
  741. X
  742. Xstatic struct xp_ops svctcp_rendezvous_op = {
  743. X    rendezvous_request,
  744. X    rendezvous_stat,
  745. X    abort,
  746. X    abort,
  747. X    abort,
  748. X    svctcp_destroy
  749. X};
  750. X
  751. Xstatic int readtcp(), writetcp();
  752. Xstatic SVCXPRT *makefd_xprt();
  753. X
  754. Xstruct tcp_rendezvous { /* kept in xprt->xp_p1 */
  755. X    u_int sendsize;
  756. X    u_int recvsize;
  757. X};
  758. X
  759. Xstruct tcp_conn {  /* kept in xprt->xp_p1 */
  760. X    enum xprt_stat strm_stat;
  761. X    u_long x_id;
  762. X    XDR xdrs;
  763. X    char verf_body[MAX_AUTH_BYTES];
  764. X};
  765. X
  766. X/*
  767. X * Usage:
  768. X *    xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
  769. X *
  770. X * Creates, registers, and returns a (rpc) tcp based transporter.
  771. X * Once *xprt is initialized, it is registered as a transporter
  772. X * see (svc.h, xprt_register).  This routine returns
  773. X * a NULL if a problem occurred.
  774. X *
  775. X * If sock<0 then a socket is created, else sock is used.
  776. X * If the socket, sock is not bound to a port then svctcp_create
  777. X * binds it to an arbitrary port.  The routine then starts a tcp
  778. X * listener on the socket's associated port.  In any (successful) case,
  779. X * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  780. X * associated port number.
  781. X *
  782. X * Since tcp streams do buffered io similar to stdio, the caller can specify
  783. X * how big the send and receive buffers are via the second and third parms;
  784. X * 0 => use the system default.
  785. X */
  786. XSVCXPRT *
  787. Xsvctcp_create(sock, sendsize, recvsize)
  788. X    register int sock;
  789. X    u_int sendsize;
  790. X    u_int recvsize;
  791. X{
  792. X    bool_t madesock = FALSE;
  793. X    register SVCXPRT *xprt;
  794. X    register struct tcp_rendezvous *r;
  795. X    struct sockaddr_in addr;
  796. X    int len = sizeof(struct sockaddr_in);
  797. X
  798. X    if (sock == RPC_ANYSOCK) {
  799. X        if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  800. X            perror("svctcp_.c - udp socket creation problem");
  801. X            return ((SVCXPRT *)NULL);
  802. X        }
  803. X        madesock = TRUE;
  804. X    }
  805. X    addr.sin_addr.s_addr = 0;
  806. X    addr.sin_family = AF_INET;
  807. X    addr.sin_port = 0;
  808. X    (void)bind(sock, (struct sockaddr *)&addr, len);
  809. X    if ((getsockname(sock, (caddr_t)&addr, &len) != 0)  ||
  810. X        (listen(sock, 2) != 0)) {
  811. X        perror("svctcp_.c - cannot getsockname or listen");
  812. X        if (madesock)
  813. X               (void)close(sock);
  814. X        return ((SVCXPRT *)NULL);
  815. X    }
  816. X    r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r));
  817. X    if (r == NULL) {
  818. X        fprintf(stderr, "svctcp_create: out of memory\n");
  819. X        return (NULL);
  820. X    }
  821. X    r->sendsize = sendsize;
  822. X    r->recvsize = recvsize;
  823. X    xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
  824. X    if (xprt == NULL) {
  825. X        fprintf(stderr, "svctcp_create: out of memory\n");
  826. X        return (NULL);
  827. X    }
  828. X    xprt->xp_p2 = NULL;
  829. X    xprt->xp_p1 = (caddr_t)r;
  830. X    xprt->xp_verf = _null_auth;
  831. X    xprt->xp_ops = &svctcp_rendezvous_op;
  832. X    xprt->xp_port = ntohs(addr.sin_port);
  833. X    xprt->xp_sock = sock;
  834. X    xprt_register(xprt);
  835. X    return (xprt);
  836. X}
  837. X
  838. X/*
  839. X * Like svtcp_create(), except the routine takes any *open* UNIX file
  840. X * descriptor as its first input.
  841. X */
  842. XSVCXPRT *
  843. Xsvcfd_create(fd, sendsize, recvsize)
  844. X    int fd;
  845. X    u_int sendsize;
  846. X    u_int recvsize;
  847. X{
  848. X
  849. X    return (makefd_xprt(fd, sendsize, recvsize));
  850. X}
  851. X
  852. Xstatic SVCXPRT *
  853. Xmakefd_xprt(fd, sendsize, recvsize)
  854. X    int fd;
  855. X    u_int sendsize;
  856. X    u_int recvsize;
  857. X{
  858. X    register SVCXPRT *xprt;
  859. X    register struct tcp_conn *cd;
  860. X    xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
  861. X    if (xprt == (SVCXPRT *)NULL) {
  862. X        fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
  863. X        goto done;
  864. X    }
  865. X    cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn));
  866. X    if (cd == (struct tcp_conn *)NULL) {
  867. X        fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n");
  868. X        mem_free(xprt, sizeof(SVCXPRT));
  869. X        xprt = (SVCXPRT *)NULL;
  870. X        goto done;
  871. X    }
  872. X    cd->strm_stat = XPRT_IDLE;
  873. X    xdrrec_create(&(cd->xdrs), sendsize, recvsize,
  874. X        (caddr_t)xprt, readtcp, writetcp);
  875. X    xprt->xp_p2 = NULL;
  876. X    xprt->xp_p1 = (caddr_t)cd;
  877. X    xprt->xp_verf.oa_base = cd->verf_body;
  878. X    xprt->xp_addrlen = 0;
  879. X    xprt->xp_ops = &svctcp_op;  /* truely deals with calls */
  880. X    xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
  881. X    xprt->xp_sock = fd;
  882. X    xprt_register(xprt);
  883. X    done:
  884. X    return (xprt);
  885. X}
  886. X
  887. Xstatic bool_t
  888. Xrendezvous_request(xprt)
  889. X    register SVCXPRT *xprt;
  890. X{
  891. X    int sock;
  892. X    struct tcp_rendezvous *r;
  893. X    struct sockaddr_in addr;
  894. X    int len;
  895. X
  896. X    r = (struct tcp_rendezvous *)xprt->xp_p1;
  897. X    again:
  898. X    len = sizeof(struct sockaddr_in);
  899. X    if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr,
  900. X        &len)) < 0) {
  901. X        if (errno == EINTR)
  902. X            goto again;
  903. X           return (FALSE);
  904. X    }
  905. X    /*
  906. X     * make a new transporter (re-uses xprt)
  907. X     */
  908. X    xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
  909. X    xprt->xp_raddr = addr;
  910. X    xprt->xp_addrlen = len;
  911. X    return (FALSE); /* there is never an rpc msg to be processed */
  912. X}
  913. X
  914. Xstatic enum xprt_stat
  915. Xrendezvous_stat()
  916. X{
  917. X
  918. X    return (XPRT_IDLE);
  919. X}
  920. X
  921. Xstatic void
  922. Xsvctcp_destroy(xprt)
  923. X    register SVCXPRT *xprt;
  924. X{
  925. X    register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1;
  926. X
  927. X    xprt_unregister(xprt);
  928. X    (void)close(xprt->xp_sock);
  929. X    if (xprt->xp_port != 0) {
  930. X        /* a rendezvouser socket */
  931. X        xprt->xp_port = 0;
  932. X    } else {
  933. X        /* an actual connection socket */
  934. X        XDR_DESTROY(&(cd->xdrs));
  935. X    }
  936. X    mem_free((caddr_t)cd, sizeof(struct tcp_conn));
  937. X    mem_free((caddr_t)xprt, sizeof(SVCXPRT));
  938. X}
  939. X
  940. X/*
  941. X * All read operations timeout after 35 seconds.
  942. X * A timeout is fatal for the connection.
  943. X */
  944. Xstatic struct timeval wait_per_try = { 35, 0 };
  945. X
  946. X/*
  947. X * reads data from the tcp conection.
  948. X * any error is fatal and the connection is closed.
  949. X * (And a read of zero bytes is a half closed stream => error.)
  950. X */
  951. Xstatic int
  952. Xreadtcp(xprt, buf, len)
  953. X    register SVCXPRT *xprt;
  954. X    caddr_t buf;
  955. X    register int len;
  956. X{
  957. X    register int sock = xprt->xp_sock;
  958. X    register int mask = 1 << sock;
  959. X    int readfds;
  960. X
  961. X    do {
  962. X        readfds = mask;
  963. X        if (select(32, &readfds, NULL, NULL, &wait_per_try) <= 0) {
  964. X            if (errno == EINTR)
  965. X                continue;
  966. X            goto fatal_err;
  967. X        }
  968. X    } while (readfds != mask);
  969. X    if ((len = read(sock, buf, len)) > 0)
  970. X        return (len);
  971. Xfatal_err:
  972. X    ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
  973. X    return (-1);
  974. X}
  975. X
  976. X/*
  977. X * writes data to the tcp connection.
  978. X * Any error is fatal and the connection is closed.
  979. X */
  980. Xstatic int
  981. Xwritetcp(xprt, buf, len)
  982. X    register SVCXPRT *xprt;
  983. X    caddr_t buf;
  984. X    int len;
  985. X{
  986. X    register int i, cnt;
  987. X
  988. X    for (cnt = len; cnt > 0; cnt -= i, buf += i) {
  989. X        if ((i = write(xprt->xp_sock, buf, cnt)) < 0) {
  990. X            ((struct tcp_conn *)(xprt->xp_p1))->strm_stat =
  991. X                XPRT_DIED;
  992. X            return (-1);
  993. X        }
  994. X    }
  995. X    return (len);
  996. X}
  997. X
  998. Xstatic enum xprt_stat
  999. Xsvctcp_stat(xprt)
  1000. X    SVCXPRT *xprt;
  1001. X{
  1002. X    register struct tcp_conn *cd =
  1003. X        (struct tcp_conn *)(xprt->xp_p1);
  1004. X
  1005. X    if (cd->strm_stat == XPRT_DIED)
  1006. X        return (XPRT_DIED);
  1007. X    if (! xdrrec_eof(&(cd->xdrs)))
  1008. X        return (XPRT_MOREREQS);
  1009. X    return (XPRT_IDLE);
  1010. X}
  1011. X
  1012. Xstatic bool_t
  1013. Xsvctcp_recv(xprt, msg)
  1014. X    SVCXPRT *xprt;
  1015. X    register struct rpc_msg *msg;
  1016. X{
  1017. X    register struct tcp_conn *cd =
  1018. X        (struct tcp_conn *)(xprt->xp_p1);
  1019. X    register XDR *xdrs = &(cd->xdrs);
  1020. X
  1021. X    xdrs->x_op = XDR_DECODE;
  1022. X    (void)xdrrec_skiprecord(xdrs);
  1023. X    if (xdr_callmsg(xdrs, msg)) {
  1024. X        cd->x_id = msg->rm_xid;
  1025. X        return (TRUE);
  1026. X    }
  1027. X    return (FALSE);
  1028. X}
  1029. X
  1030. Xstatic bool_t
  1031. Xsvctcp_getargs(xprt, xdr_args, args_ptr)
  1032. X    SVCXPRT *xprt;
  1033. X    xdrproc_t xdr_args;
  1034. X    caddr_t args_ptr;
  1035. X{
  1036. X
  1037. X    return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr));
  1038. X}
  1039. X
  1040. Xstatic bool_t
  1041. Xsvctcp_freeargs(xprt, xdr_args, args_ptr)
  1042. X    SVCXPRT *xprt;
  1043. X    xdrproc_t xdr_args;
  1044. X    caddr_t args_ptr;
  1045. X{
  1046. X    register XDR *xdrs =
  1047. X        &(((struct tcp_conn *)(xprt->xp_p1))->xdrs);
  1048. X
  1049. X    xdrs->x_op = XDR_FREE;
  1050. X    return ((*xdr_args)(xdrs, args_ptr));
  1051. X}
  1052. X
  1053. Xstatic bool_t
  1054. Xsvctcp_reply(xprt, msg)
  1055. X    SVCXPRT *xprt;
  1056. X    register struct rpc_msg *msg;
  1057. X{
  1058. X    register struct tcp_conn *cd =
  1059. X        (struct tcp_conn *)(xprt->xp_p1);
  1060. X    register XDR *xdrs = &(cd->xdrs);
  1061. X    register bool_t stat;
  1062. X
  1063. X    xdrs->x_op = XDR_ENCODE;
  1064. X    msg->rm_xid = cd->x_id;
  1065. X    stat = xdr_replymsg(xdrs, msg);
  1066. X    (void)xdrrec_endofrecord(xdrs, TRUE);
  1067. X    return (stat);
  1068. X}
  1069. SHAR_EOF
  1070. if test 9984 -ne "`wc -c < 'rpc/rpclib/svc_tcp.c'`"
  1071. then
  1072.     echo shar: "error transmitting 'rpc/rpclib/svc_tcp.c'" '(should have been 9984 characters)'
  1073. fi
  1074. chmod 444 'rpc/rpclib/svc_tcp.c'
  1075. fi
  1076. echo shar: "extracting 'rpc/rpclib/svc_udp.c'" '(6578 characters)'
  1077. if test -f 'rpc/rpclib/svc_udp.c'
  1078. then
  1079.     echo shar: "will not over-write existing file 'rpc/rpclib/svc_udp.c'"
  1080. else
  1081. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/svc_udp.c'
  1082. X/*
  1083. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  1084. X * unrestricted use provided that this legend is included on all tape
  1085. X * media and as a part of the software program in whole or part.  Users
  1086. X * may copy or modify Sun RPC without charge, but are not authorized
  1087. X * to license or distribute it to anyone else except as part of a product or
  1088. X * program developed by the user.
  1089. X * 
  1090. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  1091. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  1092. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  1093. X * 
  1094. X * Sun RPC is provided with no support and without any obligation on the
  1095. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  1096. X * modification or enhancement.
  1097. X * 
  1098. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  1099. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  1100. X * OR ANY PART THEREOF.
  1101. X * 
  1102. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  1103. X * or profits or other special, indirect and consequential damages, even if
  1104. X * Sun has been advised of the possibility of such damages.
  1105. X * 
  1106. X * Sun Microsystems, Inc.
  1107. X * 2550 Garcia Avenue
  1108. X * Mountain View, California  94043
  1109. X */
  1110. X#ifndef lint
  1111. Xstatic char sccsid[] = "@(#)svc_udp.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  1112. X#endif
  1113. X
  1114. X/*
  1115. X * svc_udp.c,
  1116. X * Server side for UDP/IP based RPC.  (Does some caching in the hopes of
  1117. X * achieving execute-at-most-once semantics.)
  1118. X *
  1119. X * Copyright (C) 1984, Sun Microsystems, Inc.
  1120. X */
  1121. X
  1122. X#include <stdio.h>
  1123. X#include "types.h"
  1124. X#include <netinet/in.h>
  1125. X#include <sys/socket.h>
  1126. X#include <errno.h>
  1127. X#include "xdr.h"
  1128. X#include "auth.h"
  1129. X#include "clnt.h"
  1130. X#include "rpc_msg.h"
  1131. X#include "svc.h"
  1132. X
  1133. Xchar *mem_alloc();
  1134. X
  1135. X#define rpc_buffer(xprt) ((xprt)->xp_p1)
  1136. X#define MAX(a, b)     ((a > b) ? a : b)
  1137. X
  1138. Xstatic bool_t        svcudp_recv();
  1139. Xstatic bool_t        svcudp_reply();
  1140. Xstatic enum xprt_stat    svcudp_stat();
  1141. Xstatic bool_t        svcudp_getargs();
  1142. Xstatic bool_t        svcudp_freeargs();
  1143. Xstatic void        svcudp_destroy();
  1144. X
  1145. Xstatic struct xp_ops svcudp_op = {
  1146. X    svcudp_recv,
  1147. X    svcudp_stat,
  1148. X    svcudp_getargs,
  1149. X    svcudp_reply,
  1150. X    svcudp_freeargs,
  1151. X    svcudp_destroy
  1152. X};
  1153. X
  1154. Xextern int errno;
  1155. X
  1156. X/*
  1157. X * kept in xprt->xp_p2
  1158. X */
  1159. Xstruct svcudp_data {
  1160. X    u_int   su_iosz;    /* byte size of send.recv buffer */
  1161. X    u_long    su_xid;        /* transaction id */
  1162. X    XDR    su_xdrs;    /* XDR handle */
  1163. X    char    su_verfbody[MAX_AUTH_BYTES];    /* verifier body */
  1164. X};
  1165. X#define    su_data(xprt)    ((struct svcudp_data *)(xprt->xp_p2))
  1166. X
  1167. X/*
  1168. X * Usage:
  1169. X *    xprt = svcudp_create(sock);
  1170. X *
  1171. X * If sock<0 then a socket is created, else sock is used.
  1172. X * If the socket, sock is not bound to a port then svcudp_create
  1173. X * binds it to an arbitrary port.  In any (successful) case,
  1174. X * xprt->xp_sock is the registered socket number and xprt->xp_port is the
  1175. X * associated port number.
  1176. X * Once *xprt is initialized, it is registered as a transporter;
  1177. X * see (svc.h, xprt_register).
  1178. X * The routines returns NULL if a problem occurred.
  1179. X */
  1180. XSVCXPRT *
  1181. Xsvcudp_bufcreate(sock, sendsz, recvsz)
  1182. X    register int sock;
  1183. X    u_int sendsz, recvsz;
  1184. X{
  1185. X    bool_t madesock = FALSE;
  1186. X    register SVCXPRT *xprt;
  1187. X    register struct svcudp_data *su;
  1188. X    struct sockaddr_in addr;
  1189. X    int len = sizeof(struct sockaddr_in);
  1190. X
  1191. X    if (sock == RPC_ANYSOCK) {
  1192. X        if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  1193. X            perror("svcudp_create: socket creation problem");
  1194. X            return ((SVCXPRT *)NULL);
  1195. X        }
  1196. X        madesock = TRUE;
  1197. X    }
  1198. X    addr.sin_addr.s_addr = 0;
  1199. X    addr.sin_family = AF_INET;
  1200. X    addr.sin_port = 0;
  1201. X    (void)bind(sock, (struct sockaddr *)&addr, len);
  1202. X    if (getsockname(sock, (caddr_t)&addr, &len) != 0) {
  1203. X        perror("svcudp_create - cannot getsockname");
  1204. X        if (madesock)
  1205. X            (void)close(sock);
  1206. X        return ((SVCXPRT *)NULL);
  1207. X    }
  1208. X    xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
  1209. X    if (xprt == NULL) {
  1210. X        fprintf(stderr, "svcudp_create: out of memory\n");
  1211. X        return (NULL);
  1212. X    }
  1213. X    su = (struct svcudp_data *)mem_alloc(sizeof(*su));
  1214. X    if (su == NULL) {
  1215. X        fprintf(stderr, "svcudp_create: out of memory\n");
  1216. X        return (NULL);
  1217. X    }
  1218. X    su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4;
  1219. X    if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) {
  1220. X        fprintf(stderr, "svcudp_create: out of memory\n");
  1221. X        return (NULL);
  1222. X    }
  1223. X    xdrmem_create(
  1224. X        &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE);
  1225. X    xprt->xp_p2 = (caddr_t)su;
  1226. X    xprt->xp_verf.oa_base = su->su_verfbody;
  1227. X    xprt->xp_ops = &svcudp_op;
  1228. X    xprt->xp_port = ntohs(addr.sin_port);
  1229. X    xprt->xp_sock = sock;
  1230. X    xprt_register(xprt);
  1231. X    return (xprt);
  1232. X}
  1233. X
  1234. XSVCXPRT *
  1235. Xsvcudp_create(sock, sendsz, recvsz)
  1236. X    int sock;
  1237. X{
  1238. X
  1239. X    return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE));
  1240. X}
  1241. Xstatic enum xprt_stat
  1242. Xsvcudp_stat(xprt)
  1243. X    SVCXPRT *xprt;
  1244. X{
  1245. X
  1246. X    return (XPRT_IDLE); 
  1247. X}
  1248. X
  1249. Xstatic bool_t
  1250. Xsvcudp_recv(xprt, msg)
  1251. X    register SVCXPRT *xprt;
  1252. X    struct rpc_msg *msg;
  1253. X{
  1254. X    register struct svcudp_data *su = su_data(xprt);
  1255. X    register XDR *xdrs = &(su->su_xdrs);
  1256. X    register int rlen;
  1257. X
  1258. X    again:
  1259. X    xprt->xp_addrlen = sizeof(struct sockaddr_in);
  1260. X    rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), su->su_iosz,
  1261. X        0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen));
  1262. X    if (rlen == -1 && errno == EINTR)
  1263. X        goto again;
  1264. X    if (rlen < 4*sizeof(u_long))
  1265. X        return (FALSE);
  1266. X    xdrs->x_op = XDR_DECODE;
  1267. X    XDR_SETPOS(xdrs, 0);
  1268. X    if (! xdr_callmsg(xdrs, msg))
  1269. X        return (FALSE);
  1270. X    su->su_xid = msg->rm_xid;
  1271. X    return (TRUE);
  1272. X}
  1273. X
  1274. Xstatic bool_t
  1275. Xsvcudp_reply(xprt, msg)
  1276. X    register SVCXPRT *xprt; 
  1277. X    struct rpc_msg *msg; 
  1278. X{
  1279. X    register struct svcudp_data *su = su_data(xprt);
  1280. X    register XDR *xdrs = &(su->su_xdrs);
  1281. X    register int slen;
  1282. X    register bool_t stat = FALSE;
  1283. X
  1284. X    xdrs->x_op = XDR_ENCODE;
  1285. X    XDR_SETPOS(xdrs, 0);
  1286. X    msg->rm_xid = su->su_xid;
  1287. X    if (xdr_replymsg(xdrs, msg)) {
  1288. X        slen = (int)XDR_GETPOS(xdrs);
  1289. X        if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0,
  1290. X            (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen)
  1291. X            == slen)
  1292. X            stat = TRUE;
  1293. X    }
  1294. X    return (stat);
  1295. X}
  1296. X
  1297. Xstatic bool_t
  1298. Xsvcudp_getargs(xprt, xdr_args, args_ptr)
  1299. X    SVCXPRT *xprt;
  1300. X    xdrproc_t xdr_args;
  1301. X    caddr_t args_ptr;
  1302. X{
  1303. X
  1304. X    return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr));
  1305. X}
  1306. X
  1307. Xstatic bool_t
  1308. Xsvcudp_freeargs(xprt, xdr_args, args_ptr)
  1309. X    SVCXPRT *xprt;
  1310. X    xdrproc_t xdr_args;
  1311. X    caddr_t args_ptr;
  1312. X{
  1313. X    register XDR *xdrs = &(su_data(xprt)->su_xdrs);
  1314. X
  1315. X    xdrs->x_op = XDR_FREE;
  1316. X    return ((*xdr_args)(xdrs, args_ptr));
  1317. X}
  1318. X
  1319. Xstatic void
  1320. Xsvcudp_destroy(xprt)
  1321. X    register SVCXPRT *xprt;
  1322. X{
  1323. X    register struct svcudp_data *su = su_data(xprt);
  1324. X
  1325. X    xprt_unregister(xprt);
  1326. X    (void)close(xprt->xp_sock);
  1327. X    XDR_DESTROY(&(su->su_xdrs));
  1328. X    mem_free(rpc_buffer(xprt), su->su_iosz);
  1329. X    mem_free((caddr_t)su, sizeof(struct svcudp_data));
  1330. X    mem_free((caddr_t)xprt, sizeof(SVCXPRT));
  1331. X}
  1332. SHAR_EOF
  1333. if test 6578 -ne "`wc -c < 'rpc/rpclib/svc_udp.c'`"
  1334. then
  1335.     echo shar: "error transmitting 'rpc/rpclib/svc_udp.c'" '(should have been 6578 characters)'
  1336. fi
  1337. chmod 444 'rpc/rpclib/svc_udp.c'
  1338. fi
  1339. echo shar: "extracting 'rpc/rpclib/xdr.c'" '(10032 characters)'
  1340. if test -f 'rpc/rpclib/xdr.c'
  1341. then
  1342.     echo shar: "will not over-write existing file 'rpc/rpclib/xdr.c'"
  1343. else
  1344. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/xdr.c'
  1345. X/*
  1346. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  1347. X * unrestricted use provided that this legend is included on all tape
  1348. X * media and as a part of the software program in whole or part.  Users
  1349. X * may copy or modify Sun RPC without charge, but are not authorized
  1350. X * to license or distribute it to anyone else except as part of a product or
  1351. X * program developed by the user.
  1352. X * 
  1353. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  1354. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  1355. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  1356. X * 
  1357. X * Sun RPC is provided with no support and without any obligation on the
  1358. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  1359. X * modification or enhancement.
  1360. X * 
  1361. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  1362. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  1363. X * OR ANY PART THEREOF.
  1364. X * 
  1365. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  1366. X * or profits or other special, indirect and consequential damages, even if
  1367. X * Sun has been advised of the possibility of such damages.
  1368. X * 
  1369. X * Sun Microsystems, Inc.
  1370. X * 2550 Garcia Avenue
  1371. X * Mountain View, California  94043
  1372. X */
  1373. X#ifndef lint
  1374. Xstatic char sccsid[] = "@(#)xdr.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  1375. X#endif
  1376. X
  1377. X/*
  1378. X * xdr.c, Generic XDR routines implementation.
  1379. X *
  1380. X * Copyright (C) 1984, Sun Microsystems, Inc.
  1381. X *
  1382. X * These are the "generic" xdr routines used to serialize and de-serialize
  1383. X * most common data items.  See xdr.h for more info on the interface to
  1384. X * xdr.
  1385. X */
  1386. X
  1387. X#include "types.h"
  1388. X#include "xdr.h"
  1389. X#include <stdio.h>
  1390. Xchar *malloc();
  1391. X
  1392. X/*
  1393. X * constants specific to the xdr "protocol"
  1394. X */
  1395. X#define XDR_FALSE    ((long) 0)
  1396. X#define XDR_TRUE    ((long) 1)
  1397. X#define LASTUNSIGNED    ((u_int) 0-1)
  1398. X
  1399. X/*
  1400. X * for unit alignment
  1401. X */
  1402. Xstatic char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
  1403. X
  1404. X
  1405. X/*
  1406. X * XDR nothing
  1407. X */
  1408. Xbool_t
  1409. Xxdr_void(/* xdrs, addr */)
  1410. X    /* XDR *xdrs; */
  1411. X    /* caddr_t addr; */
  1412. X{
  1413. X
  1414. X    return (TRUE);
  1415. X}
  1416. X
  1417. X/*
  1418. X * XDR integers
  1419. X */
  1420. Xbool_t
  1421. Xxdr_int(xdrs, ip)
  1422. X    XDR *xdrs;
  1423. X    int *ip;
  1424. X{
  1425. X
  1426. X#ifdef lint
  1427. X    (void) (xdr_short(xdrs, (short *)ip));
  1428. X    return (xdr_long(xdrs, (long *)ip));
  1429. X#else
  1430. X    if (sizeof (int) == sizeof (long)) {
  1431. X        return (xdr_long(xdrs, (long *)ip));
  1432. X    } else {
  1433. X        return (xdr_short(xdrs, (short *)ip));
  1434. X    }
  1435. X#endif
  1436. X}
  1437. X
  1438. X/*
  1439. X * XDR unsigned integers
  1440. X */
  1441. Xbool_t
  1442. Xxdr_u_int(xdrs, up)
  1443. X    XDR *xdrs;
  1444. X    u_int *up;
  1445. X{
  1446. X
  1447. X#ifdef lint
  1448. X    (void) (xdr_short(xdrs, (short *)up));
  1449. X    return (xdr_u_long(xdrs, (u_long *)up));
  1450. X#else
  1451. X    if (sizeof (u_int) == sizeof (u_long)) {
  1452. X        return (xdr_u_long(xdrs, (u_long *)up));
  1453. X    } else {
  1454. X        return (xdr_short(xdrs, (short *)up));
  1455. X    }
  1456. X#endif
  1457. X}
  1458. X
  1459. X/*
  1460. X * XDR long integers
  1461. X * same as xdr_u_long - open coded to save a proc call!
  1462. X */
  1463. Xbool_t
  1464. Xxdr_long(xdrs, lp)
  1465. X    register XDR *xdrs;
  1466. X    long *lp;
  1467. X{
  1468. X
  1469. X    if (xdrs->x_op == XDR_ENCODE)
  1470. X        return (XDR_PUTLONG(xdrs, lp));
  1471. X
  1472. X    if (xdrs->x_op == XDR_DECODE)
  1473. X        return (XDR_GETLONG(xdrs, lp));
  1474. X
  1475. X    if (xdrs->x_op == XDR_FREE)
  1476. X        return (TRUE);
  1477. X
  1478. X    return (FALSE);
  1479. X}
  1480. X
  1481. X/*
  1482. X * XDR unsigned long integers
  1483. X * same as xdr_long - open coded to save a proc call!
  1484. X */
  1485. Xbool_t
  1486. Xxdr_u_long(xdrs, ulp)
  1487. X    register XDR *xdrs;
  1488. X    u_long *ulp;
  1489. X{
  1490. X
  1491. X    if (xdrs->x_op == XDR_DECODE)
  1492. X        return (XDR_GETLONG(xdrs, (long *)ulp));
  1493. X    if (xdrs->x_op == XDR_ENCODE)
  1494. X        return (XDR_PUTLONG(xdrs, (long *)ulp));
  1495. X    if (xdrs->x_op == XDR_FREE)
  1496. X        return (TRUE);
  1497. X    return (FALSE);
  1498. X}
  1499. X
  1500. X/*
  1501. X * XDR short integers
  1502. X */
  1503. Xbool_t
  1504. Xxdr_short(xdrs, sp)
  1505. X    register XDR *xdrs;
  1506. X    short *sp;
  1507. X{
  1508. X    long l;
  1509. X
  1510. X    switch (xdrs->x_op) {
  1511. X
  1512. X    case XDR_ENCODE:
  1513. X        l = (long) *sp;
  1514. X        return (XDR_PUTLONG(xdrs, &l));
  1515. X
  1516. X    case XDR_DECODE:
  1517. X        if (!XDR_GETLONG(xdrs, &l)) {
  1518. X            return (FALSE);
  1519. X        }
  1520. X        *sp = (short) l;
  1521. X        return (TRUE);
  1522. X
  1523. X    case XDR_FREE:
  1524. X        return (TRUE);
  1525. X    }
  1526. X    return (FALSE);
  1527. X}
  1528. X
  1529. X/*
  1530. X * XDR unsigned short integers
  1531. X */
  1532. Xbool_t
  1533. Xxdr_u_short(xdrs, usp)
  1534. X    register XDR *xdrs;
  1535. X    u_short *usp;
  1536. X{
  1537. X    u_long l;
  1538. X
  1539. X    switch (xdrs->x_op) {
  1540. X
  1541. X    case XDR_ENCODE:
  1542. X        l = (u_long) *usp;
  1543. X        return (XDR_PUTLONG(xdrs, &l));
  1544. X
  1545. X    case XDR_DECODE:
  1546. X        if (!XDR_GETLONG(xdrs, &l)) {
  1547. X            return (FALSE);
  1548. X        }
  1549. X        *usp = (u_short) l;
  1550. X        return (TRUE);
  1551. X
  1552. X    case XDR_FREE:
  1553. X        return (TRUE);
  1554. X    }
  1555. X    return (FALSE);
  1556. X}
  1557. X
  1558. X
  1559. X/*
  1560. X * XDR booleans
  1561. X */
  1562. Xbool_t
  1563. Xxdr_bool(xdrs, bp)
  1564. X    register XDR *xdrs;
  1565. X    bool_t *bp;
  1566. X{
  1567. X    long lb;
  1568. X
  1569. X    switch (xdrs->x_op) {
  1570. X
  1571. X    case XDR_ENCODE:
  1572. X        lb = *bp ? XDR_TRUE : XDR_FALSE;
  1573. X        return (XDR_PUTLONG(xdrs, &lb));
  1574. X
  1575. X    case XDR_DECODE:
  1576. X        if (!XDR_GETLONG(xdrs, &lb)) {
  1577. X            return (FALSE);
  1578. X        }
  1579. X        *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
  1580. X        return (TRUE);
  1581. X
  1582. X    case XDR_FREE:
  1583. X        return (TRUE);
  1584. X    }
  1585. X    return (FALSE);
  1586. X}
  1587. X
  1588. X/*
  1589. X * XDR enumerations
  1590. X */
  1591. Xbool_t
  1592. Xxdr_enum(xdrs, ep)
  1593. X    XDR *xdrs;
  1594. X    enum_t *ep;
  1595. X{
  1596. X    enum sizecheck { SIZEVAL };    /* used to find the size of an enum */
  1597. X
  1598. X    /*
  1599. X     * enums are treated as ints
  1600. X     */
  1601. X#ifdef lint
  1602. X    (void) (xdr_short(xdrs, (short *)ep));
  1603. X    return (xdr_long(xdrs, (long *)ep));
  1604. X#else
  1605. X    if (sizeof (enum sizecheck) == sizeof (long)) {
  1606. X        return (xdr_long(xdrs, (long *)ep));
  1607. X    } else if (sizeof (enum sizecheck) == sizeof (short)) {
  1608. X        return (xdr_short(xdrs, (short *)ep));
  1609. X    } else {
  1610. X        return (FALSE);
  1611. X    }
  1612. X#endif
  1613. X}
  1614. X
  1615. X/*
  1616. X * XDR opaque data
  1617. X * Allows the specification of a fixed size sequence of opaque bytes.
  1618. X * cp points to the opaque object and cnt gives the byte length.
  1619. X */
  1620. Xbool_t
  1621. Xxdr_opaque(xdrs, cp, cnt)
  1622. X    register XDR *xdrs;
  1623. X    caddr_t cp;
  1624. X    register u_int cnt;
  1625. X{
  1626. X    register u_int rndup;
  1627. X    static crud[BYTES_PER_XDR_UNIT];
  1628. X
  1629. X    /*
  1630. X     * if no data we are done
  1631. X     */
  1632. X    if (cnt == 0)
  1633. X        return (TRUE);
  1634. X
  1635. X    /*
  1636. X     * round byte count to full xdr units
  1637. X     */
  1638. X    rndup = cnt % BYTES_PER_XDR_UNIT;
  1639. X    if (rndup > 0)
  1640. X        rndup = BYTES_PER_XDR_UNIT - rndup;
  1641. X
  1642. X    if (xdrs->x_op == XDR_DECODE) {
  1643. X        if (!XDR_GETBYTES(xdrs, cp, cnt)) {
  1644. X            return (FALSE);
  1645. X        }
  1646. X        if (rndup == 0)
  1647. X            return (TRUE);
  1648. X        return (XDR_GETBYTES(xdrs, crud, rndup));
  1649. X    }
  1650. X
  1651. X    if (xdrs->x_op == XDR_ENCODE) {
  1652. X        if (!XDR_PUTBYTES(xdrs, cp, cnt)) {
  1653. X            return (FALSE);
  1654. X        }
  1655. X        if (rndup == 0)
  1656. X            return (TRUE);
  1657. X        return (XDR_PUTBYTES(xdrs, xdr_zero, rndup));
  1658. X    }
  1659. X
  1660. X    if (xdrs->x_op == XDR_FREE) {
  1661. X        return (TRUE);
  1662. X    }
  1663. X
  1664. X    return (FALSE);
  1665. X}
  1666. X
  1667. X/*
  1668. X * XDR counted bytes
  1669. X * *cpp is a pointer to the bytes, *sizep is the count.
  1670. X * If *cpp is NULL maxsize bytes are allocated
  1671. X */
  1672. Xbool_t
  1673. Xxdr_bytes(xdrs, cpp, sizep, maxsize)
  1674. X    register XDR *xdrs;
  1675. X    char **cpp;
  1676. X    register u_int *sizep;
  1677. X    u_int maxsize;
  1678. X{
  1679. X    register char *sp = *cpp;  /* sp is the actual string pointer */
  1680. X    register u_int nodesize;
  1681. X
  1682. X    /*
  1683. X     * first deal with the length since xdr bytes are counted
  1684. X     */
  1685. X    if (! xdr_u_int(xdrs, sizep)) {
  1686. X        return (FALSE);
  1687. X    }
  1688. X    nodesize = *sizep;
  1689. X    if ((nodesize > maxsize) && (xdrs->x_op != XDR_FREE)) {
  1690. X        return (FALSE);
  1691. X    }
  1692. X
  1693. X    /*
  1694. X     * now deal with the actual bytes
  1695. X     */
  1696. X    switch (xdrs->x_op) {
  1697. X
  1698. X    case XDR_DECODE:
  1699. X        if (sp == NULL) {
  1700. X            *cpp = sp = (char *)mem_alloc(nodesize);
  1701. X        }
  1702. X        if (sp == NULL) {
  1703. X            fprintf(stderr, "xdr_bytes: out of memory\n");
  1704. X            return (FALSE);
  1705. X        }
  1706. X        /* fall into ... */
  1707. X
  1708. X    case XDR_ENCODE:
  1709. X        return (xdr_opaque(xdrs, sp, nodesize));
  1710. X
  1711. X    case XDR_FREE:
  1712. X        if (sp != NULL) {
  1713. X            mem_free(sp, nodesize);
  1714. X            *cpp = NULL;
  1715. X        }
  1716. X        return (TRUE);
  1717. X    }
  1718. X    return (FALSE);
  1719. X}
  1720. X
  1721. X/*
  1722. X * XDR a descriminated union
  1723. X * Support routine for discriminated unions.
  1724. X * You create an array of xdrdiscrim structures, terminated with
  1725. X * an entry with a null procedure pointer.  The routine gets
  1726. X * the discriminant value and then searches the array of xdrdiscrims
  1727. X * looking for that value.  It calls the procedure given in the xdrdiscrim
  1728. X * to handle the discriminant.  If there is no specific routine a default
  1729. X * routine may be called.
  1730. X * If there is no specific or default routine an error is returned.
  1731. X */
  1732. Xbool_t
  1733. Xxdr_union(xdrs, dscmp, unp, choices, dfault)
  1734. X    register XDR *xdrs;
  1735. X    enum_t *dscmp;        /* enum to decide which arm to work on */
  1736. X    caddr_t unp;        /* the union itself */
  1737. X    struct xdr_discrim *choices;    /* [value, xdr proc] for each arm */
  1738. X    xdrproc_t dfault;    /* default xdr routine */
  1739. X{
  1740. X    register enum_t dscm;
  1741. X
  1742. X    /*
  1743. X     * we deal with the discriminator;  it's an enum
  1744. X     */
  1745. X    if (! xdr_enum(xdrs, dscmp)) {
  1746. X        return (FALSE);
  1747. X    }
  1748. X    dscm = *dscmp;
  1749. X
  1750. X    /*
  1751. X     * search choices for a value that matches the discriminator.
  1752. X     * if we find one, execute the xdr routine for that value.
  1753. X     */
  1754. X    for (; choices->proc != NULL_xdrproc_t; choices++) {
  1755. X        if (choices->value == dscm)
  1756. X            return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED));
  1757. X    }
  1758. X
  1759. X    /*
  1760. X     * no match - execute the default xdr routine if there is one
  1761. X     */
  1762. X    return ((dfault == NULL_xdrproc_t) ? FALSE :
  1763. X        (*dfault)(xdrs, unp, LASTUNSIGNED));
  1764. X}
  1765. X
  1766. X
  1767. X/*
  1768. X * Non-portable xdr primitives.
  1769. X * Care should be taken when moving these routines to new architectures.
  1770. X */
  1771. X
  1772. X
  1773. X/*
  1774. X * XDR null terminated ASCII strings
  1775. X * xdr_string deals with "C strings" - arrays of bytes that are
  1776. X * terminated by a NULL character.  The parameter cpp references a
  1777. X * pointer to storage; If the pointer is null, then the necessary
  1778. X * storage is allocated.  The last parameter is the max allowed length
  1779. X * of the string as specified by a protocol.
  1780. X */
  1781. Xbool_t
  1782. Xxdr_string(xdrs, cpp, maxsize)
  1783. X    register XDR *xdrs;
  1784. X    char **cpp;
  1785. X    u_int maxsize;
  1786. X{
  1787. X    register char *sp = *cpp;  /* sp is the actual string pointer */
  1788. X    u_int size;
  1789. X    u_int nodesize;
  1790. X
  1791. X    /*
  1792. X     * first deal with the length since xdr strings are counted-strings
  1793. X     */
  1794. X    if ((xdrs->x_op) != XDR_DECODE)
  1795. X        size = strlen(sp);
  1796. X    if (! xdr_u_int(xdrs, &size)) {
  1797. X        return (FALSE);
  1798. X    }
  1799. X    if (size > maxsize) {
  1800. X        return (FALSE);
  1801. X    }
  1802. X    nodesize = size + 1;
  1803. X
  1804. X    /*
  1805. X     * now deal with the actual bytes
  1806. X     */
  1807. X    switch (xdrs->x_op) {
  1808. X
  1809. X    case XDR_DECODE:
  1810. X        if (sp == NULL)
  1811. X            *cpp = sp = (char *)mem_alloc(nodesize);
  1812. X        if (sp == NULL) {
  1813. X            fprintf(stderr, "xdr_string: out of memory\n");
  1814. X            return (FALSE);
  1815. X        }
  1816. X        sp[size] = 0;
  1817. X        /* fall into ... */
  1818. X
  1819. X    case XDR_ENCODE:
  1820. X        return (xdr_opaque(xdrs, sp, size));
  1821. X
  1822. X    case XDR_FREE:
  1823. X        if (sp != NULL) {
  1824. X            mem_free(sp, nodesize);
  1825. X            *cpp = NULL;
  1826. X        }
  1827. X        return (TRUE);
  1828. X    }
  1829. X    return (FALSE);
  1830. X}
  1831. X
  1832. X/* 
  1833. X * Wrapper for xdr_string that can be called directly from 
  1834. X * routines like clnt_call
  1835. X */
  1836. Xbool_t
  1837. Xxdr_wrapstring(xdrs, cpp)
  1838. X    XDR *xdrs;
  1839. X    char **cpp;
  1840. X{
  1841. X    if (xdr_string(xdrs, cpp, BUFSIZ)) {
  1842. X        return(TRUE);
  1843. X    }
  1844. X    return(FALSE);
  1845. X}
  1846. SHAR_EOF
  1847. if test 10032 -ne "`wc -c < 'rpc/rpclib/xdr.c'`"
  1848. then
  1849.     echo shar: "error transmitting 'rpc/rpclib/xdr.c'" '(should have been 10032 characters)'
  1850. fi
  1851. chmod 444 'rpc/rpclib/xdr.c'
  1852. fi
  1853. echo shar: "extracting 'rpc/rpclib/xdr_array.c'" '(3497 characters)'
  1854. if test -f 'rpc/rpclib/xdr_array.c'
  1855. then
  1856.     echo shar: "will not over-write existing file 'rpc/rpclib/xdr_array.c'"
  1857. else
  1858. sed 's/^X//' << \SHAR_EOF > 'rpc/rpclib/xdr_array.c'
  1859. X/*
  1860. X * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  1861. X * unrestricted use provided that this legend is included on all tape
  1862. X * media and as a part of the software program in whole or part.  Users
  1863. X * may copy or modify Sun RPC without charge, but are not authorized
  1864. X * to license or distribute it to anyone else except as part of a product or
  1865. X * program developed by the user.
  1866. X * 
  1867. X * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  1868. X * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  1869. X * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  1870. X * 
  1871. X * Sun RPC is provided with no support and without any obligation on the
  1872. X * part of Sun Microsystems, Inc. to assist in its use, correction,
  1873. X * modification or enhancement.
  1874. X * 
  1875. X * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  1876. X * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  1877. X * OR ANY PART THEREOF.
  1878. X * 
  1879. X * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  1880. X * or profits or other special, indirect and consequential damages, even if
  1881. X * Sun has been advised of the possibility of such damages.
  1882. X * 
  1883. X * Sun Microsystems, Inc.
  1884. X * 2550 Garcia Avenue
  1885. X * Mountain View, California  94043
  1886. X */
  1887. X#ifndef lint
  1888. Xstatic char sccsid[] = "@(#)xdr_array.c 1.1 86/02/03 Copyr 1984 Sun Micro";
  1889. X#endif
  1890. X
  1891. X/*
  1892. X * xdr_array.c, Generic XDR routines impelmentation.
  1893. X *
  1894. X * Copyright (C) 1984, Sun Microsystems, Inc.
  1895. X *
  1896. X * These are the "non-trivial" xdr primitives used to serialize and de-serialize
  1897. X * arrays.  See xdr.h for more info on the interface to xdr.
  1898. X */
  1899. X
  1900. X#include "types.h"
  1901. X#include "xdr.h"
  1902. X#include <stdio.h>
  1903. Xchar *mem_alloc();
  1904. X#define LASTUNSIGNED    ((u_int)0-1)
  1905. X
  1906. X
  1907. X/*
  1908. X * XDR an array of arbitrary elements
  1909. X * *addrp is a pointer to the array, *sizep is the number of elements.
  1910. X * If addrp is NULL (*sizep * elsize) bytes are allocated.
  1911. X * elsize is the size (in bytes) of each element, and elproc is the
  1912. X * xdr procedure to call to handle each element of the array.
  1913. X */
  1914. Xbool_t
  1915. Xxdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc)
  1916. X    register XDR *xdrs;
  1917. X    caddr_t *addrp;        /* array pointer */
  1918. X    u_int *sizep;        /* number of elements */
  1919. X    u_int maxsize;        /* max numberof elements */
  1920. X    u_int elsize;        /* size in bytes of each element */
  1921. X    xdrproc_t elproc;    /* xdr routine to handle each element */
  1922. X{
  1923. X    register u_int i;
  1924. X    register caddr_t target = *addrp;
  1925. X    register u_int c;  /* the actual element count */
  1926. X    register bool_t stat = TRUE;
  1927. X    register int nodesize;
  1928. X
  1929. X    /* like strings, arrays are really counted arrays */
  1930. X    if (! xdr_u_int(xdrs, sizep)) {
  1931. X        return (FALSE);
  1932. X    }
  1933. X    c = *sizep;
  1934. X    if ((c > maxsize) && (xdrs->x_op != XDR_FREE)) {
  1935. X        return (FALSE);
  1936. X    }
  1937. X    nodesize = c * elsize;
  1938. X
  1939. X    /*
  1940. X     * if we are deserializing, we may need to allocate an array.
  1941. X     * We also save time by checking for a null array if we are freeing.
  1942. X     */
  1943. X    if (target == NULL)
  1944. X        switch (xdrs->x_op) {
  1945. X        case XDR_DECODE:
  1946. X            if (c == 0)
  1947. X                return (TRUE);
  1948. X            *addrp = target = mem_alloc(nodesize);
  1949. X            if (target == NULL) {
  1950. X                fprintf(stderr, "xdr_array: out of memory\n");
  1951. X                return (FALSE);
  1952. X            }
  1953. X            bzero(target, (u_int)nodesize);
  1954. X            break;
  1955. X
  1956. X        case XDR_FREE:
  1957. X            return (TRUE);
  1958. X    }
  1959. X    
  1960. X    /*
  1961. X     * now we xdr each element of array
  1962. X     */
  1963. X    for (i = 0; (i < c) && stat; i++) {
  1964. X        stat = (*elproc)(xdrs, target, LASTUNSIGNED);
  1965. X        target += elsize;
  1966. X    }
  1967. X
  1968. X    /*
  1969. X     * the array may need freeing
  1970. X     */
  1971. X    if (xdrs->x_op == XDR_FREE) {
  1972. X        mem_free(*addrp, nodesize);
  1973. X        *addrp = NULL;
  1974. X    }
  1975. X    return (stat);
  1976. X}
  1977. SHAR_EOF
  1978. if test 3497 -ne "`wc -c < 'rpc/rpclib/xdr_array.c'`"
  1979. then
  1980.     echo shar: "error transmitting 'rpc/rpclib/xdr_array.c'" '(should have been 3497 characters)'
  1981. fi
  1982. chmod 444 'rpc/rpclib/xdr_array.c'
  1983. fi
  1984. exit 0
  1985. #    End of shell archive
  1986.  
  1987.